package cn.chinaflame.pagodas.Result;

import com.google.common.base.Strings;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * Created by jia on 2015/7/26.
 */
public class Requests {

    public static HttpServletRequest getRequest() {
        RequestAttributes ra = RequestContextHolder.currentRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        return sra.getRequest();
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static HttpSession getSession(boolean isCreate) {
        return getRequest().getSession(isCreate);
    }

    public static boolean isAjaxRequest() {
        return getRequest().getHeader("X-REQUESTED-WITH") == null ? false : true;
    }

    public static void printCookie() {
        HttpServletRequest req = getRequest();
        Enumeration<String> headers = req.getHeaderNames();
        while (headers.hasMoreElements()) {
            String name = headers.nextElement();
            System.out.println("header name :" + name + "; header value :" + req.getHeader(name));
        }

        Cookie[] cookies = getRequest().getCookies();
        if (cookies != null)
            for (int i = 0; i < cookies.length; i++) {
                System.out.println("cookie name:" + cookies[i].getName() + ";value:" + cookies[i].getValue());
            }
        HttpSession session = getRequest().getSession();
        System.out.println(session);
    }


    public static String getSessionIdByCookie() {
        Cookie[] cookies = getRequest().getCookies();
        for (Cookie cookie : cookies) {
            if ("JSESSIONID".equalsIgnoreCase(cookie.getName()))
                return cookie.getValue();
            else if ("SESSION".equalsIgnoreCase(cookie.getName()))
                return cookie.getValue();
        }
        return null;
    }

    public static String getRequestAddr() {
        HttpServletRequest req = getRequest();
        String ip = req.getHeader("x-forwarded-for");
        if (!Strings.isNullOrEmpty(ip)) {
            ip = ip.split(",")[0].trim();
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = req.getHeader("Proxy-Client-IP");
            System.out.println("ip1:" + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = req.getHeader("WL-Proxy-Client-IP");
            System.out.println("ip2:" + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = req.getRemoteAddr();
            System.out.println("ip3:" + ip);
        }
        return ip;
    }
}
